/*
*	Created:			06.03.10
*	Author:				009
*	Last Modifed:		-
*/

// SDK
#include "SDK/amx/amx.h"
#include "SDK/plugincommon.h"

#include <stdio.h>
#include <time.h>

#include "os.h"

//AMX*	gAMX;
time_t LastUpdate = -1;

//typedef void (*logprintf_t)(char* format, ...);
//logprintf_t logprintf;

//void **ppPluginData;
extern void *pAMXFunctions;

void NativesOnAMXLoad(AMX *amx);


THREAD_IDENTIFY(AntiSleepThreadHandle);

THREAD_START(AntiSleepThread)
{
	while(true)
	{
		SLEEP(70);
		if(LastUpdate == -1) continue;
		
		time_t curtime;
		time(&curtime);
		
		if((curtime - LastUpdate) > (80))
		{
			FILE * fp = fopen("crash.txt", "a");
			fprintf(fp, "%s | %ld | %ld\n", ctime(&curtime), LastUpdate, curtime);
			fclose(fp);
			fprintf(stdout, "hang on: %s | %ld | %ld\n", ctime(&curtime), LastUpdate, curtime);
			exit(1);
		}
	}
THREAD_END

PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports() 
{
	return SUPPORTS_VERSION | SUPPORTS_AMX_NATIVES;
}

PLUGIN_EXPORT bool PLUGIN_CALL Load( void **ppData ) 
{
	pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS];
	//logprintf = (logprintf_t)ppData[PLUGIN_DATA_LOGPRINTF];

	//logprintf("================");
	//logprintf("AntiSleep v " PLUGIN_VERSION);
	//logprintf("by 009");
	//logprintf("http://samp.club42.ru");
	//logprintf("================");
	// config
	//ReadConfig();
	// thread
	THREAD_RESULT(ThreadId);
	CREATE_THREAD(AntiSleepThreadHandle,AntiSleepThread,ThreadId);
	return true;
}

PLUGIN_EXPORT void PLUGIN_CALL Unload( )
{
	//logprintf("================");
	//logprintf("Useful functions v " PLUGIN_VERSION);
	//logprintf("by 009");
	//logprintf("http://samp.club42.ru");
	//logprintf("================");
}

PLUGIN_EXPORT int PLUGIN_CALL AmxLoad( AMX *amx ) 
{
	//gAMX = amx;
	NativesOnAMXLoad(amx);
	return 1;
}

PLUGIN_EXPORT int PLUGIN_CALL AmxUnload( AMX *amx ) 
{
	return AMX_ERR_NONE;
}
